package com.smooth.site.sx.pc.job.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.smooth.common.site.common.enums.SiteCodeEnums;
import com.smooth.common.site.common.excep.SiteException;
import com.smooth.dao.gen.BizJobApplayMapper;
import com.smooth.dao.gen.bean.BizCompany;
import com.smooth.dao.gen.bean.BizJob;
import com.smooth.dao.gen.bean.BizJobApplay;
import com.smooth.dao.gen.bean.BizJobApplayExample;
import com.smooth.dao.gen.bean.BizStudent;
import com.smooth.site.sx.mag.biz.applyjob.service.BizJobApplayService;
import com.smooth.site.sx.mag.biz.compcollege.comp.service.PcCollegeCompService;
import com.smooth.site.sx.mag.biz.job.service.BizJobService;
import com.smooth.site.sx.mag.biz.student.service.BizStudentService;
import com.smooth.site.sx.pc.job.enums.ApplyJobStatus;
import com.smooth.site.sx.pc.job.enums.ApplyJobType;
import com.smooth.site.sx.pc.job.req.apply.JobApplyQueryReq;
import com.smooth.site.sx.pc.job.req.apply.JobApplyReq;
import com.smooth.sys.pc.vo.ListRsp;

@Component
public class PcJobApplyService {
	Logger log = LoggerFactory.getLogger(PcJobApplyService.class);
	@Autowired
	BizJobApplayService bizJobApplayService;
	@Autowired
	BizStudentService bizStudentService;
	@Autowired
	PcCollegeCompService pcCollegeCompService;
	@Autowired
	BizJobService bizJobService;
	@Autowired
	BizJobApplayMapper bizJobApplayMapper;
	public BizJobApplay applyJob(JobApplyReq jobApplyReq,BizStudent bizStudent) {
		BizJob bizJob = bizJobService.get(jobApplyReq.getJobId());
		Date date = new Date();
		BizJobApplay bizJobApplay  = queryApplyByJobid(jobApplyReq.getJobId(),bizStudent.getId());
		if(bizJobApplay==null) {
			bizJobApplay = new BizJobApplay();
			bizJobApplay.setApplayStatus(ApplyJobStatus.applying.value);
			bizJobApplay.setCompId(bizJob.getCompId());
			bizJobApplay.setCompName(bizJob.getCompName());
			bizJobApplay.setCreateDate(date);
			bizJobApplay.setJobId(jobApplyReq.getJobId());
			bizJobApplay.setJobTitle(bizJob.getJobTitle());
			bizJobApplay.setStuId(bizStudent.getId());
			bizJobApplay.setStuName(bizStudent.getStuName());
			bizJobApplay.setStuTelNo(bizStudent.getTelNo());
			bizJobApplay.setUpdateDate(date);
			bizJobApplay.setApplyType(ApplyJobType.jobappy.value);
			bizJobApplay.setCollegeId(bizStudent.getSchId());
			bizJobApplay.setCollegeName(bizStudent.getSchName());
			bizJobApplay = bizJobApplayService.save(bizJobApplay);
		}else {
			log.info(jobApplyReq.getJobId()+" 已经存在 直接跳过");
			throw new SiteException(SiteCodeEnums.JOB_REAPPLY, bizJobApplay.getJobTitle());
		}
		return bizJobApplay;
	}
	
	@Transactional
	public BizJobApplay optApplyJob(String applyId,Integer applyStatus,BizCompany bizCompany) {
		BizJobApplay bizJobApplay = bizJobApplayService.get(applyId);
		ApplyJobStatus applyJobStatus = ApplyJobStatus.getStatus(bizJobApplay.getApplayStatus());
		ApplyJobStatus applyReqJobStatus = ApplyJobStatus.getStatus(applyStatus);
		if(applyReqJobStatus==null) {
			throw new SiteException(SiteCodeEnums.param_error, "请填写申请状态");
		}
		if(ApplyJobStatus.applying.equals(applyJobStatus)  && (ApplyJobStatus.applysuccess.equals(applyReqJobStatus)
				||ApplyJobStatus.applyfailed.equals(applyReqJobStatus))) {
			
			BizJobApplay bizJobApplayRecord = new BizJobApplay();
			bizJobApplayRecord.setId(applyId);
			bizJobApplayRecord.setApplayStatus(applyReqJobStatus.value);
			
			bizJobApplay = bizJobApplayService.save(bizJobApplayRecord);
			
			if(ApplyJobStatus.applysuccess.equals(applyReqJobStatus)) {
				
			}
			return bizJobApplay;
		}else {
			throw new SiteException(SiteCodeEnums.param_error, "申请状态异常");
		}
	}
	
	
	public BizJobApplay queryApplyByJobid(String jobId,String stuId) {
		BizJobApplayExample bizJobApplayExample = new BizJobApplayExample();
		
		BizJobApplayExample.Criteria criteria = bizJobApplayExample.createCriteria();
		criteria.andJobIdEqualTo(jobId);
		criteria.andStuIdEqualTo(stuId);
		criteria.andApplyTypeEqualTo(ApplyJobType.jobappy.value);
		List<BizJobApplay> blist = bizJobApplayMapper.selectByExample(bizJobApplayExample);
		if(blist.size()>0) {
			return blist.get(0);
		}
		return null;
	}
	
	public ListRsp<BizJobApplay> queryStuApplyJobs(JobApplyQueryReq jobApplyQueryReq,BizStudent bizStudent) {
		ListRsp<BizJobApplay> brsp= new ListRsp<BizJobApplay>();
		BizJobApplayExample bizJobApplayExample = new BizJobApplayExample();
		BizJobApplayExample.Criteria criteria = bizJobApplayExample.createCriteria();
		if(jobApplyQueryReq.getApplyStatus()!=null) {
			if(ApplyJobStatus.compsendaccept.value.equals(jobApplyQueryReq.getApplyStatus())) {
				List<Integer> ilist = new ArrayList<Integer>();
				ilist.add(ApplyJobStatus.compsendaccept.value);
				ilist.add(ApplyJobStatus.offerCreated.value);
				criteria.andApplayStatusIn(ilist);
			}else {
				criteria.andApplayStatusEqualTo(jobApplyQueryReq.getApplyStatus());
			}
		}else {
			criteria.andApplayStatusNotEqualTo(ApplyJobStatus.offerCreated.value);
		}
		criteria.andStuIdEqualTo(bizStudent.getId());
		
		criteria.andApplyTypeEqualTo(ApplyJobType.jobappy.value);
		bizJobApplayExample.setOrderByClause(" create_date desc ");
		List<BizJobApplay>  blist = bizJobApplayMapper.selectByExample(bizJobApplayExample);
		brsp.setDatalist(blist);
		brsp.setTsize(bizJobApplayMapper.countByExample(bizJobApplayExample));
		
		return brsp;
	}
	
	public ListRsp<BizJobApplay> queryCompApplyJobs(JobApplyQueryReq jobApplyQueryReq,BizCompany bizCompany) {
		ListRsp<BizJobApplay> brsp= new ListRsp<BizJobApplay>();
		BizJobApplayExample bizJobApplayExample = new BizJobApplayExample();
		BizJobApplayExample.Criteria criteria = bizJobApplayExample.createCriteria();
		if(jobApplyQueryReq.getApplyStatus()!=null) {
			if(ApplyJobStatus.compsendaccept.value.equals(jobApplyQueryReq.getApplyStatus())) {
				List<Integer> ilist = new ArrayList<Integer>();
				ilist.add(ApplyJobStatus.compsendaccept.value);
				ilist.add(ApplyJobStatus.offerCreated.value);
				criteria.andApplayStatusIn(ilist);
			}else {
				criteria.andApplayStatusEqualTo(jobApplyQueryReq.getApplyStatus());
			}
		}
		criteria.andCompIdEqualTo(bizCompany.getId());
		
		criteria.andApplyTypeEqualTo(ApplyJobType.jobappy.value);
		bizJobApplayExample.setOrderByClause(" create_date desc ");
		List<BizJobApplay>  blist = bizJobApplayMapper.selectByExample(bizJobApplayExample);
		brsp.setDatalist(blist);
		brsp.setTsize(bizJobApplayMapper.countByExample(bizJobApplayExample));
		
		return brsp;
	}
	
	
	
}
